package com.huoshan.im.server;

import com.huoshan.im.server.config.ConfigHolder;
import com.huoshan.im.server.config.properties.SettingConfig;
import lombok.extern.slf4j.Slf4j;
import online.chazi.netkit.NetkitContext;
import online.chazi.netkit.NetkitServer;
import org.springframework.stereotype.Component;

/**
 * 注意：特定针对云主机进行编码！
 * （1）不支持docker容器化运行
 * （2）如果是物理机运行及对外网提供服务，需要指定外网ip
 * （3）支持多张网卡的高io云主机机型
 * Created by 火山九日 on 2020-04-25 17:40
 * 火山九日
 */
@Slf4j
@Component
public class ChatServer extends BaseChatServer {

    private static NetkitServer imServer;
    private static NetkitServer wsServer;

    public void startup() {

        ConfigHolder.setSettingConfig(settingConfig);
        SettingConfig.Server server = settingConfig.getServer();

        SessionHolder.buildNetkitContext(server.getCore());
        NetkitContext context = SessionHolder.getContext();
        init(context);

        imServer = new NetkitServer(context, server.getHost(), server.getPort());
        imServer.setMaxConnections(server.getMaxConnections());
        imServer.setStartUpListener(success -> {
            if (success) {
                log.info("Server up at {}:{}", server.getHost(), server.getPort());
                cacheServer();
            }
        });
        imServer.startup();

    }

    public void shutdown() {
        imServer.shutdown();
    }

}
